package com.cjgl.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cjgl.common.BaseResponse;
import com.cjgl.common.ErrorCode;
import com.cjgl.common.ResultUtils;
import com.cjgl.exception.BusinessException;
import com.cjgl.model.dto.score.ChooseCourseRequest;
import com.cjgl.model.dto.student.ChooseCourseRequestAdd;
import com.cjgl.model.entity.*;
import com.cjgl.model.enums.SelectCourseOpenTypeEnum;
import com.cjgl.service.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static com.cjgl.constant.UserConstant.USER_LOGIN_STATE;

/**
 * 实现学生的选课功能
 * 简单实现一下（因为数据库中，没有对课程进行分类 比如公选课，某个学院的专业课，只有某个学院的学生才能选的课程）
 * 需要向 拿到了 List<TblCourses>  获取courseNo --> 然后从session获取 studentNo -->向 scores表中天添加数据courseNo studentNo
 * <p>
 * -- 因为没有设计在courses中设计字段课程类型 courseType 所以设计表 coursetype(courseNo courseType)
 * 建立视图 courseSelectView(courseInfos,courseType)
 * 从试图查 操作基本表
 *
 * @author zhangyuwu
 */

/**
 * 去他妈的随便去 课程表拉点信息出来实现了算了
 *
 * @author zhangyuwu
 */

@Slf4j
@RestController
@RequestMapping("/StudentChooseCoursesController")
public class StudentChooseCoursesController {
	/**
	 * 课程
	 */

	@Resource
	TblCoursesService coursesService;

	/**
	 * 成绩
	 */

	@Resource
	TblScoresService scoresService;

	@Resource
	TblCourseselectionService courseselectionService;

	@Resource
	TblCoursestableService coursestableService;


	/**
	 * 因该从course中获取公选课或者
	 * 还是比较垃圾的没有条件查询
	 *
	 * @param request
	 * @return
	 */
	@GetMapping("/getCourses")
	public BaseResponse<List<TblCourses>> getCourses(HttpServletRequest request) {
		// login
		TblStudents student = (TblStudents) request.getSession().getAttribute(USER_LOGIN_STATE);
		if (student == null) {
			throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR);
		}

		//是否开启选课
		LambdaQueryWrapper<TblCourseselection> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(TblCourseselection::getCourseType, SelectCourseOpenTypeEnum.PUBLIC_COURSE.getValue());
		TblCourseselection chooseCourseIsOpen = courseselectionService.getBaseMapper().selectOne(queryWrapper);

		if (chooseCourseIsOpen.getIsCourseSelection() != SelectCourseOpenTypeEnum.IS_OPEN.getValue()) {
			return ResultUtils.error(ErrorCode.NO_AUTH_ERROR, "暂未开启选课");
		}


		log.info("查询所有此学生" + student.getStudentNo() + "==" + student.getStudentName() + "没有选过的课程");
		//查课程
		List<TblCourses> tblCourses = coursesService.getNotSelectedCourse(student.getStudentNo());

		//应该是不会为空的
		if (ObjectUtils.isEmpty(tblCourses)) {
			tblCourses = new ArrayList<>();
		}

		return ResultUtils.success(tblCourses);
	}


	/**
	 * 向score表插入
	 * 记得提交以后重新请求 getCourses
	 *
	 * @param courses
	 * @param request
	 * @return
	 */
	@Transactional
	@GetMapping("/addCourses")
//	public BaseResponse<Object> addCourses(@RequestBody List<ChooseCourseRequest> courses, HttpServletRequest request) {
	public BaseResponse<Object> addCourses(@RequestParam("courses") String courses, HttpServletRequest request) {
		// login
		TblStudents student = (TblStudents) request.getSession().getAttribute(USER_LOGIN_STATE);
		if (student == null) {
			throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR);
		}



		String studentNo = student.getStudentNo();

		// 应该先有老师确定教什么课,在向courseTable中添加信息后,然后让学生选去选

//		courses.forEach(item -> {
			TblScores scoreInsert = new TblScores();
			scoreInsert.setId(null);
			scoreInsert.setStudentNo(studentNo);
			scoreInsert.setCourseNo(courses);
			scoreInsert.setScore(0D);
//			scoreInsert.setTerm(item.getTerm());
			// 向score表插入
			int insertCheck = scoresService.getBaseMapper().insert(scoreInsert);
			if (insertCheck <= 0) {
				throw new BusinessException(ErrorCode.OPERATION_ERROR);
			}

//			coursestableService.getBaseMapper().insert()
//		});

		// 实现的有点垃圾


		return ResultUtils.success(insertCheck);
	}

}
